package com.game.springbootstart.controller.user;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.springbootstart.config.QueryPageParam;
import com.game.springbootstart.config.Result;
import com.game.springbootstart.entity.Category;
import com.game.springbootstart.entity.Games;
import com.game.springbootstart.service.IGamesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 * 游戏表 前端控制器
 * </p>
 *
 * @author author
 * @since 2025-01-29
 */
@RestController
@RequestMapping("/games")
public class GamesController {
    @Autowired
    private IGamesService gamesService;
    @GetMapping("/listId")
    public Result listGames(@RequestParam Long id){
        return Result.success(gamesService.listId(id));
    }
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('用户')")
    public Result list() {
        return Result.success(gamesService.listByStatus());
    }
    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query) {
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        Page<Games> page = new Page<>();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());
        LambdaQueryWrapper<Games> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Games::getName,name)
                .eq(Games::getStatus,"上线");
        IPage<Games> result = gamesService.page(page, queryWrapper);
        return Result.success(result.getRecords(), result.getTotal());
    }
    @GetMapping("/getCategories")
    public Result getCategories() {
        return Result.success(gamesService.getCategories());
    }
    @PostMapping("/selectCategory")
    public Result selectCategoryByName(@RequestParam String categoryName) {
        return Result.success(gamesService.selectCategoryByName(categoryName));
    }
    @PostMapping("/search")
    public Result searchGames(@RequestParam String keyword) {
        return Result.success(gamesService.searchGames(keyword));
    }
}
